package com.comtop.topcloud.device.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.comtop.topcloud.device.model.Command;
import com.comtop.topcloud.device.model.influxdb.DeviceReportingDataModel;
import com.comtop.topcloud.device.model.influxdb.GatewayUsageModel;
import lombok.extern.slf4j.Slf4j;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ResultMapper {

    public static GatewayUsageModel mapJsonToGatewayUsageModel(JSONObject jsonObject) {

        GatewayUsageModel model = new GatewayUsageModel();


        // 解析字段并设置到 GatewayUsageModel 对应属性中
        Long timestamp = jsonObject.getLong("ts");
        model.setCommFlowM(jsonObject.getInteger("commFlowM"));
        model.setCommFlowY(jsonObject.getInteger("commFlowY"));
        model.setCpu(jsonObject.getString("cpu"));
        model.setDiskUsed(jsonObject.getString("diskUsed"));
        model.setMemUsed(jsonObject.getString("memUsed"));
        model.setSeq(jsonObject.getInteger("seq"));
        model.setSignal(jsonObject.getInteger("signal"));
        model.setMonitorName(jsonObject.getString("name"));
        model.setPKey(jsonObject.getString("pKey"));
        model.setSn(jsonObject.getString("sn"));
        model.setTs(timestamp);

        // 处理时间戳转换
        model.setTime(LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()));

        return model;
    }

    public static List<DeviceReportingDataModel> mapJsonToDeviceReportingDataModels(JSONObject jsonObject) {
        List<DeviceReportingDataModel> models = new ArrayList<>();
        JSONArray devsJsonArray = jsonObject.getJSONArray("devs");
        JSONObject dataJSONObject = jsonObject.getJSONObject("data");
        String productKey = jsonObject.getString("pKey");
        String serialNumber = jsonObject.getString("sn");
        String version = jsonObject.getString("ver");
        Long timestamp = jsonObject.getLong("ts");

        for (int i = 0; devsJsonArray != null && i < devsJsonArray.size(); i++) {
            JSONObject devJson = devsJsonArray.getJSONObject(i);
            String monitorName = devJson.getString("dev");
            JSONArray dJsonArray = devJson.getJSONArray("d");

            for (int j = 0; j < dJsonArray.size(); j++) {
                JSONObject dataJson = dJsonArray.getJSONObject(j);
                String tagName = dataJson.getString("m");
                timestamp = dataJson.getLong("ts");
                Double val = dataJson.getDouble("v");
                Integer dataQuality = dataJson.getInteger("dq");

                DeviceReportingDataModel model = new DeviceReportingDataModel();
                model.setMonitorName(monitorName);
                model.setTagName(tagName);
                model.setVal(val);
                model.setDataQuality(dataQuality);
                model.setVersion(version);
                model.setProductKey(productKey);
                model.setSerialNumber(serialNumber);
                model.setTimestamp(timestamp);

                // 将 Unix 时间戳转换为 LocalDateTime
                model.setTime(LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()));

                models.add(model);
            }
        }
        if (dataJSONObject != null) {
            String monitorName = dataJSONObject.getString("dev");
            String tagName = dataJSONObject.getString("m");
            String remark = dataJSONObject.getString("remark");
            String sysId = dataJSONObject.getString("sysid");
            Double val = dataJSONObject.getDouble("v");
            Integer valId = dataJSONObject.getInteger("valid");

            DeviceReportingDataModel model = new DeviceReportingDataModel();
            model.setDeviceId(serialNumber+"/"+monitorName);
            model.setTagName(tagName);
            model.setVal(val);
            model.setVersion(version);
            model.setProductKey(productKey);
            model.setSerialNumber(serialNumber);
            model.setTimestamp(timestamp);


            // 将 Unix 时间戳转换为 LocalDateTime
            //model.setTime(Instant.ofEpochSecond(timestamp).atZone(ZoneId.systemDefault()).toInstant());
            models.add(model);
        }
        return models;
    }

    public static Command mapJsonToCommand(JSONObject jsonObject) {
        JSONObject dataJSONObject = jsonObject.getJSONObject("data");
        String productKey = jsonObject.getString("pKey");
        String serialNumber = jsonObject.getString("sn");
        String version = jsonObject.getString("ver");
        Long timestamp = jsonObject.getLong("ts");
        Command model = new Command();

        if (dataJSONObject != null) {
            String monitorName = dataJSONObject.getString("dev");
            String tagName = dataJSONObject.getString("m");
            String remark = dataJSONObject.getString("remark");
            String sysId = dataJSONObject.getString("sysid");
            String val = dataJSONObject.getString("v");
            Integer valId = dataJSONObject.getInteger("valid");

            Command.Data data = new Command.Data();
            data.setMonitorName(monitorName);
            data.setM(tagName);
            data.setV(val);
            data.setSysid(sysId);

            model.setData(data);
            model.setVer(version);
            model.setPKey(productKey);
            model.setSn(serialNumber);
            model.setTs(timestamp);
            model.setRemark(remark);
            model.setValid(valId);
        }
        return model;
    }




}